package com.xwaf.platform.web.controller.x;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.api.entity.netty.WebSocketNettyUsers;
import com.xwaf.platform.common.api.message.MailDto;
import com.xwaf.platform.common.api.message.kafka.KafkaTestMsg;
import com.xwaf.platform.common.api.message.kafka.KafkaTopic;
import com.xwaf.platform.common.kafka.KafkaMqService;
import com.xwaf.platform.common.rabbit.collector.MailSendCollector;
import com.xwaf.platform.common.socket.WebSocketServer;
import com.xwaf.platform.system.api.entity.DataBase;
import com.xwaf.platform.system.api.service.SpringMailService;

import io.swagger.annotations.Api;
import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@Api(hidden = true)
@RestController("消息服务相关")
@RequestMapping("/x/info")
public class InformationController {

	@Autowired
	MailSendCollector sendCollector;

	@Autowired
	KafkaMqService mqService;

	@Autowired
	SpringMailService springMailService;// 邮件服务

	/**
	 * 邮件服务
	 */
	@GetMapping("/testEmail")
	public CommonResponse testEmail() throws Exception {
		MailDto mailDto = new MailDto();
		mailDto.setFrom(null);
		mailDto.setTo(new String[] { "xwaf_love@yeah.net" });
		mailDto.setCc(null);
		mailDto.setSubject("test");// 主题
		mailDto.setTextContent("test context");// 邮件内容
		springMailService.sendSimpleTextMail(mailDto);
		System.out.println("我已经发送完成");
		return CommonResponse.createCommonResponse();
	}

	// http://localhost:19102/x/mq/email
	@RequestMapping(value = "/email", produces = { "application/json;charset=UTF-8" })
	public void email() throws Exception {
		MailDto mailDto = new MailDto();
		mailDto.setFrom(null);
		mailDto.setTo(new String[] { "xwaf_love@yeah.net" });
		mailDto.setCc(null);
		mailDto.setSubject("test");// 主题
		mailDto.setTextContent("test context");// 邮件内容
		sendCollector.sendMsg(mailDto);
	}

	// http://localhost:19102/x/mq/kafkaTest
	@RequestMapping(value = "/kafkaTest", produces = { "application/json;charset=UTF-8" })
	public void kafkaTest() throws Exception {
		KafkaTestMsg msg = new KafkaTestMsg();
		msg.setOrderId(DataBase.getDataBaseId(DataBase.MQTT));
		msg.setTotalFee(100L);
		msg.setMsg("msg的一些消息");
		mqService.sendKafkaMsg(KafkaTopic.KAFKATEST, JSON.toJSONString(msg));
	}

	/**
	 * 
	 * <p>
	 * 推送数据接口
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月30日 上午9:34:46 <br>
	 * @see 无<br>
	 * @param cid
	 * @param message
	 * @return
	 * @demo <br>
	 */
	@RequestMapping("/push/{cid}")
	public String pushToWeb(@PathVariable String cid, String message) {
		try {
			WebSocketServer.sendInfo(message, cid);
		} catch (IOException e) {
			e.printStackTrace();
			return cid + "#" + e.getMessage();
		}
		return cid;
	}

	/**
	 * 
	 * <p>
	 * netty 服务
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2020年1月9日 下午8:48:54 <br>
	 * @see 无<br>
	 * @param cid
	 * @param message
	 * @return
	 * @demo <br>
	 */
	@RequestMapping("/nettyPush/{userName}")
	public String nettyPushToWeb(@PathVariable String userName, String message) {
		WebSocketNettyUsers.sendMessageToUser(userName, message);
		return userName;
	}
}
